package servlet;

import model.Goods;
import model.Category;
import service.CategoryService;
import service.GoodsService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

//分类查询商品
@WebServlet(name = "goods_List",urlPatterns = "/goods_list")
public class GoodsListServlet extends HttpServlet {
    private GoodsService gService = new GoodsService();
    private CategoryService tService = new CategoryService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = 0;
        //若categoryId不为空，则获取categoryId
        if (request.getParameter("categoryId") != null) {
            id = Integer.parseInt(request.getParameter("categoryId"));
        }
        Category c = null;
        if (id != 0) {
            c = tService.selectTypeNameByID(id);
        }
        request.setAttribute("id", id);

        List<Goods> list= gService.selectGoodsByTypeID(id);
        request.setAttribute("productList", list);

        request.getRequestDispatcher("/goods_list.jsp").forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
